﻿using PlayListSet.Entity;
using PlayListSet.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace PlayListSet.Comm
{
    internal class AppControl : System.ComponentModel.INotifyPropertyChanged
    {
        private bool isEditModel=false;

        public bool IsEditModel
        {
            set { isEditModel = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsEditModel")); }
            get { return isEditModel; }
        }
        private string logMsg = null;

        public string LogMsg
        {
            set { logMsg = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LogMsg")); }
            get { return logMsg; }
        }
        private static  AppControl appControl;
      
        public TimeCodePlayContorl2 TimeCodePlayContorl { set; get; }
        public OSCSendControl OSCSendControl { private set; get; }
        public PlayCMDControl PlayCMDControl {  private set; get; }
        public InputControl InputControl {  private set; get; }
        public List<UserPlayInfoCmd> UserPlayInfoCmds { set; get; }
        private AppControl()
        {
            AppConfig = AppConfig.Read();
          
        }
        public static AppControl Instance {
            get {
                if (appControl == null)
                {
                    appControl = new AppControl();
                }
                return appControl;
            }
         }

        public event PropertyChangedEventHandler PropertyChanged;
        public AppConfig AppConfig {  get; set; }
        public List<ChannelInfo> ChannelInfos { set; get; }
        public Dictionary<string, ObservableCollection<PlayInfoBase>> PlayInfoMap { set; get; }
    
        public PlayInfoBase CopySelect { set; get; }
        public MainWindow MainWindow { set; get; }
        public void Init(MainWindow mainWindow)
        {
            MainWindow = mainWindow;
            //TimeCodePlayContorl = new TimeCodePlayContorl(dispatcher);
            TimeCodePlayContorl = new TimeCodePlayContorl2();

            PlayCMDControl = new PlayCMDControl();
            try
            {
                OSCSendControl = new OSCSendControl();
                OSCSendControl.Open();
            }
            catch (Exception ex) { 
                MessageBox.Show("OSC 初始化失败："+ex.Message);
            }
           
            UserPlayInfoCmds = UserPlayInfoCmd.Read();

            InputControl = new InputControl();
            InputControl.Open();
            //LocalAudioPlays = new List<LocalAudioPlay>();
            //for (int i = 0; i < ChannelInfos.Count; i++)
            //{
            //    if (ChannelInfos[i].ChannelType == 1)
            //    {
            //        LocalAudioPlays.Add(
            //            new LocalAudioPlay(ChannelInfos[i].ChannelName, dispatcher)
            //            );
            //    }
            //}
        }

        public void Close()
        {
            OSCSendControl.Dispose();
            TimeCodePlayContorl.Stop();
            try
            {
                InputControl.Close();
            }
            catch (Exception)
            {
                 
            }
        }
        public void Log(string msg)
        {
            LogMsg = DateTime.Now.ToString("HH:mm:ss ") + msg;
            VCLib.Util.LogHelp.Write(msg);
        }
    }
}
